//
//  CZDetailCell.swift
//  GZWeibo666
//
//  Created by wzz on 15/12/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZDetailCell: UITableViewCell {
    
    var detailStatus: CZDetailMessage? {
        didSet {
            // 当别人设置status属性时,来设置顶部视图内容
            
            // 设置评论头像
            if let iconUrl = detailStatus?.user?.profile_image_url {
                
                // 使用SDWebImage下载头像
                iconView.sd_setImageWithURL(NSURL(string:iconUrl), placeholderImage: UIImage(named: "avatar"))
            }
           
            // 设置用户名称
            // = 右边可以使用?是因为nameLabel.text是一个可选
            nameLabel.text = detailStatus?.user?.screen_name
            
            // 时间
            timeLabel.text = detailStatus?.created_at
            
            contentLabel.text = detailStatus?.text
            
            
            
                }
        }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
//    
//    // MARK: - 计算行高
//    /// 计算行高
    func rowHeight(detailStatus: CZDetailMessage) -> CGFloat {
        // 根据模型来设置内容, 当我们把传入进来的模型赋值给self.status cell里面的内容被设置成为status的内容
        self.detailStatus = detailStatus

        // 根据新的内容更新约束
        self.layoutIfNeeded()
        
        // 获取底部视图的最大Y值(cell的高度)
        return CGRectGetMaxY(contentLabel.frame)
    }
    func prepareUI() {
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(contentLabel)
        
        
        /// 用户头像
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: 35, height: 35), offset: CGPoint(x: CZStatusCellMargin, y: CZStatusCellMargin))
        
        /// 用户名称
    
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        //发表评论时间
        timeLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: nameLabel, size: nil
            , offset:  CGPoint(x: 0, y: CZStatusCellMargin))
        
        // 评论文本内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size:nil
        , offset:  CGPoint(x: 0, y: CZStatusCellMargin))
        
//        /// 会员图标
//        mbrankImageView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: CZStatusCellMargin, y: 0))
//        contentLabel.ff_AlignHorizontal(type: ff_AlignType.BottomLeft, referView: self, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
//        contentLabel.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: <#T##CGFloat#>))
        
        
    }
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
    
    /// 时间
    private lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 9)

    /// 评论文本内容
   private lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
        
        // 设置换行
        label.numberOfLines = 0
        label.sizeToFit()
//        label.bounds.width  =  UIScreen.mainScreen().bounds.size.width - iconView.bounds.width - CZStatusCellMargin
        return label
    }()
    
//    /// 会员等级图标
//    private lazy var mbrankImageView: UIImageView = UIImageView()
//    
//    /// 认证类型图标
//    private lazy var verifiedImageView:  UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
//    
//    /// 顶部分割视图
//    private lazy var separatorView: UIView = {
//        let view = UIView()
//    
//        // 背景
//        view.backgroundColor = UIColor(white: 0.9, alpha: 1)
//        
//        return view
//    }()
}

